package com.sixfoursystems.wave.mke;

import java.util.*;

import com.google.wave.api.*;

@SuppressWarnings("serial")
public class WaveBotServlet extends AbstractRobotServlet implements Quotes {

	@Override
	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();
		for(Event e : bundle.getEvents()) {
			if(e.getType() == EventType.WAVELET_BLIP_CREATED) {
				
			} else if(e.getType() == EventType.BLIP_SUBMITTED) {
				
			} else if(e.getType() == EventType.WAVELET_SELF_ADDED) {
				int r = (int)Math.floor(Math.random() * QUOTES.length);
				Blip blip = wavelet.appendBlip();
				TextView textView = blip.getDocument();
				textView.append(QUOTES[r]);
			} else if(e.getType() == EventType.WAVELET_PARTICIPANTS_CHANGED) {
				String imgBase = "http://grasshopperpebbles.com/demos/assets/js/jquery/imbannerrotater/";
				Collection<String> p = e.getAddedParticipants();
				Iterator<String> i = p.iterator();
				Blip blip = null;
				String[] names = {
						"joneil",
						"scarter",
						"djackson",
						"tealc",
						"cmitchell",
						"vala"
				};
				while(i.hasNext()) {
					String s = i.next();
					if(s.equals("mkewavebot@appspot.com"))
						continue;
					if(blip == null)
						blip = wavelet.appendBlip();
					TextView textView = blip.getDocument();
					textView.append("Welcome, " + s + "\n");
					int r = (int)Math.floor(Math.random() * names.length);
					String imgUrl = imgBase + names[r];
					Image img = new Image(imgUrl + ".jpg", 100, 100, "");
					textView.appendElement(img);
				}
			} /*else {
				Blip b = wavelet.appendBlip();
				TextView tv = b.getDocument();
				tv.append("EventType = " + e.getType());
			}*/
		}
	}
}
